package com.global.college.ui.fragment.home;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.graphics.Paint;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.OvershootInterpolator;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.global.college.R;
import com.global.college.base.BaseFragment;
import com.global.college.base.baselist.HsScrollViewPagerAdapter;
import com.global.college.data.net.HomeGoodListBean;
import com.global.college.views.RoundCornerImageview;
import com.global.college.widget.tab.SlidingTabLayout;
import com.global.college.widget.tab.listener.OnTabSelectListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class HomeFragment extends BaseFragment {
    @BindView(R.id.mSlidingTab)
    SlidingTabLayout mSlidingTab;
    @BindView(R.id.viewPager)
    ViewPager viewPager;
    @BindView(R.id.rl_train_float)
    RelativeLayout rlFloatTrain;
    @BindView(R.id.rl_breath)
    RelativeLayout rlBreath;
    @BindView(R.id.riv_float_img)
    RoundCornerImageview rivFloatImg;
    @BindView(R.id.tv_float_price)
    TextView tvFloatPrice;
    @BindView(R.id.tv_float_origin_price)
    TextView tvFloatOriginPrice;
    @BindView(R.id.iv_breath)
    ImageView ivBreath;

    private List<Fragment> mFragments = new ArrayList<>();
    private List<String> mTitles = new ArrayList<>();
    private int currentPosition = 0;

    private HomeSelectFragment homeSelectFragment;

    private boolean isFloatVisible = true;

    @Override
    public int getContentViewId() {
        return R.layout.fragment_home;
    }

    @Override
    public void initView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        initTab();
        setListener();
        tvFloatOriginPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
        ObjectAnimator breathAnimator = ObjectAnimator.ofFloat(ivBreath, "alpha", 1, 0, 1);
        breathAnimator.setDuration(2000).setRepeatCount(ValueAnimator.INFINITE);
        breathAnimator.setRepeatMode(ValueAnimator.RESTART);
        breathAnimator.start();
    }

    @Override
    protected void initData() {

    }

    private void setListener() {
        mSlidingTab.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                viewPager.setCurrentItem(position);
                currentPosition = position;
            }

            @Override
            public void onTabReselect(int position) {
            }
        });
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                currentPosition = position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
    }

    private void initTab() {
        List<String> strs = new ArrayList<>();
        mTitles.add("精选");
        mTitles.add("训练营");
        mTitles.add("应用路径");
        mTitles.add("和数说");
        for (int i = 0; i < mTitles.size(); i++) {
            strs.add(mTitles.get(i));
        }
        homeSelectFragment = new HomeSelectFragment();
        mFragments.add(homeSelectFragment);
        mFragments.add(WebviewFragment.newInstance("http://h5.uton.net"));//训练营
        mFragments.add(WebviewFragment.newInstance("http://h5.uton.net/application"));//应用路径
        mFragments.add(WebviewFragment.newInstance("http://h5.uton.net/numberTheory"));//和数说

        HsScrollViewPagerAdapter adapter = new HsScrollViewPagerAdapter(mActivity.getSupportFragmentManager(), mFragments, strs);
        viewPager.setAdapter(adapter);
        mSlidingTab.setTabSpaceEqual(true);
        mSlidingTab.setViewPager(viewPager);
        viewPager.setCurrentItem(currentPosition);
        viewPager.setOffscreenPageLimit(3);
    }

    public void jumpTrain(){
        homeSelectFragment.jumpTrain();
    }

    public void onGetTrain(HomeGoodListBean beans){
        double trainPrice = beans.getGoodList().get(0).getGoodPrice() - beans.getGoodList().get(0).getGoodPreferentialPrice();
        tvFloatPrice.setText("￥" + String.format("%.2f", trainPrice));
        tvFloatOriginPrice.setText("￥" + String.format("%.2f",beans.getGoodList().get(0).getGoodPrice()));
        Glide.with(mActivity).load(beans.getGoodList().get(0).getTrainingModel().getCover()).into(rivFloatImg);
    }

    @OnClick({R.id.btn_resign,R.id.rl_train_float, R.id.iv_float_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_float_btn:
                if (isFloatVisible) {
                    rlFloatTrain.animate()
                            .translationX(mActivity.getWindowManager().getDefaultDisplay().getWidth())
                            .setDuration(200);
                    isFloatVisible = false;
                } else {
                    ObjectAnimator animator = ObjectAnimator.ofFloat(
                            rlFloatTrain, "translationX", 0);
                    animator.setInterpolator(new OvershootInterpolator());
                    animator.setDuration(500);
                    animator.start();
                    isFloatVisible = true;
                }
                break;
            case R.id.btn_resign:
            case R.id.rl_train_float:
                homeSelectFragment.jumpTrain();
                break;
        }
    }

    public void setVisible(){
        rlFloatTrain.setVisibility(View.VISIBLE);
        rlBreath.setVisibility(View.VISIBLE);
    }
}
